<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="设备ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipid">
              <a-input v-model="model.equipid" placeholder="请输入设备ID"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参数名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入参数名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上公差" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lowLimit">
              <a-input-number v-model="model.lowLimit" placeholder="请输入上公差" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="下公差" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upLimit">
              <a-input-number v-model="model.upLimit" placeholder="请输入下公差" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="基准值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="standard">
              <a-input-number v-model="model.standard" placeholder="请输入基准值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数据填写类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="datatype">
              <j-dict-select-tag type="list" v-model="model.datatype" dictCode="qis_matdetail_cfg@DATATYPE"
                                 placeholder="请选择数据填写类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否check" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="limitCheck">
              <j-dict-select-tag type="list" v-model="model.limitCheck" dictCode="isCheck"
                                 placeholder="请选择是否check" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="isValid" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="排序序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">
              <a-input v-model="model.sequence" placeholder="请输入排序序号"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'

export default {
  name: 'EqpSpchkCfgForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      id: '',
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        recId: [
          { required: true, message: '请输入主键ID!' }
        ],
        equipid: [
          { required: true, message: '请输入设备ID!' }
        ],
        datatype: [
          { required: true, message: '请输入数据填写类型!' }
        ]
      },
      url: {
        add: '/eqp/eqpSpchkCfg/add',
        edit: '/eqp/eqpSpchkCfg/edit',
        queryById: '/eqp/eqpSpchkCfg/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      //在这里赋值联合主键 设置禁用与修改条件
      this.id = this.model.recId
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          //这里判断新增还是修改
          if (!this.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>
